﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlanWater.Helper.Entity;
using log4net;
using System.Reflection;

namespace PlanWater.Helper
{
  
     public class LogHelper
    {
         
         /// <summary>
         /// 单例
         /// </summary>
         public static readonly LogHelper Instance = new LogHelper();

         //日志对象 
         private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

         private LogHelper() {}
         /// <summary>
         /// 日志消息队列
         /// </summary>
         private static Queue<LogMessage> _message = new Queue<LogMessage>();

         /// <summary>
         /// 消息数量
         /// </summary>
         public int LogCount { get { return _message.Count; } }
         /// <summary>
         /// 插入消息
         /// </summary>
         /// <param name="msg"></param>
         public void Log(string msg)
         {
             Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fffff"), msg);
         }
         /// <summary>
         /// 插入消息
         /// </summary>
         /// <param name="time"></param>
         /// <param name="msg"></param>
         public void Log(string time, string msg)
         {
             LogMessage message = new LogMessage() { Time = time, Message = msg };
             Log(message);
         }

         /// <summary>
         /// 插入消息
         /// </summary>
         /// <param name="msg"></param>
         public  void Log(LogMessage msg) {
             _message.Enqueue(msg);
         }

         /// <summary>
         /// 输出消息
         /// </summary>
         /// <returns></returns>
         public string OutPutMessage() {
             string msg = string.Empty;
             if (_message.Count > 0 )
             {
                 var value= _message.Dequeue();
                 msg = Environment.NewLine + value.Time + Environment.NewLine + value.Message;
                 logger.Info(msg);
             }
             return msg;
         }
    }
}
